package com.cloud.api.controller;
import com.cloud.api.model.User;
import com.cloud.fillte.annotation.LogAnnotation;
import com.cloud.api.service.UserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.EntityNotFoundException;
import java.util.Optional;

@RestController
@LogAnnotation//自定义拦截注解
@RequestMapping("hello")
public class Hello {
    @Value("${content}")
    String content;
    @Value("${server.port}")
    String port;

    @Autowired
    UserService userService;


    @ApiOperation(value="获取用户详细信息", notes="根据url的id来获取用户详细信息")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer", paramType = "path")
    @RequestMapping("/port")
    public  User home(@RequestParam String  id) {
        User user =userService.findById(id);
        Optional.ofNullable(user).orElseThrow(() -> new EntityNotFoundException("id 为 " + id + " 的用户不存在"));
        return user;
    }
    @ExceptionHandler(EntityNotFoundException.class)
    public ResponseEntity<String> handleException(EntityNotFoundException ex) {
        return new ResponseEntity<>(ex.getMessage(), HttpStatus.NOT_FOUND);
    }

}
